package it.unibz.inf.cn;

import java.net.URI;
import java.net.URLConnection;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.signature.SignatureMethod;

public class Autorization {

	 private OAuthConsumer consumer = null;
	 private OAuthProvider provider = null;
	
	public Autorization() throws Exception {
		
		consumer = new DefaultOAuthConsumer(
                "y3tRbSiGa3aNOYZEoCrFOw",
                "ZQaJ8MeTdgDdXihGFrIVkumHBrAg7q2HWUu8g4Vx4iA",
                SignatureMethod.HMAC_SHA1);
		
		provider = new DefaultOAuthProvider(consumer,
                "http://twitter.com/oauth/request_token",
                "http://twitter.com/oauth/access_token",
                "http://twitter.com/oauth/authorize");
	}
	
	public Autorization(String token, String tokenSecret) throws Exception {
		this();
		consumer.setTokenWithSecret(token, tokenSecret);
	}
	
	public URI getRegistrationUri() throws Exception {
		return new URI(provider.retrieveRequestToken(OAuth.OUT_OF_BAND));
	}
	
	public String[] retrieveAccessToken(String pin) throws Exception {
		System.out.println(pin);
		provider.retrieveAccessToken(pin);
		String[] token = new String[2];
		token[0] = consumer.getToken();
		token[1] = consumer.getTokenSecret();
		return token;
	}
	
	public void sign(URLConnection conn) throws Exception {
		consumer.sign(conn);
	}
	
}
